package com.qdxwx.common.utils;


import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具类
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public class DateUtils {

    public static void main(String[] args) {
        System.out.println(DateUtils.toString(LocalDate.now(), "yyyy-MM"));
    }

    /**
     * 获取当前时间, 默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    @Override
    public String toString() {
        return toString("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当前时间 指定格式
     *
     * @param pattern 格式
     * @return 指定格式
     */
    public static String toString(String pattern) {
        return toString(LocalDateTime.now(), pattern);
    }

    /**
     * 获取当前时间 指定格式
     *
     * @param time 时间
     * @return 指定格式
     */
    public static String toString(LocalDateTime time) {
        return toString(time, "yyyy-MM-dd HH:mm:ss");
    }


    public static String toString(LocalDate time, String pattern) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        return time.format(fmt);
    }

    /**
     * 获取指定时间格式
     *
     * @param time    时间
     * @param pattern 格式
     * @return 格式
     */
    public static String toString(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取指定时间格式
     *
     * @param date    时间
     * @param pattern 格式
     * @return 格式
     */
    public static String toString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.format(date);
        } catch (Exception ex) {
            return "";
        }
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return "yyyy-MM-dd"
     */
    public static String getDate() {
        return toString("yyyy-MM-dd");
    }

    /**
     * 获取当前时间, 默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getTime() {
        return toString("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static String datePath() {
        return toString("yyyyMM");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static String dateTime() {
        return toString("yyyyMMdd");
    }

    /**
     * 获取服务器启动时间
     */
    public static LocalDateTime getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        Instant instant = Instant.ofEpochMilli(time);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(LocalDateTime beginTime, LocalDateTime endTime) {
        return disTime(beginTime, endTime);
    }

    public static String disTime(LocalDateTime beginTime, LocalDateTime endTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = Duration.between(beginTime, endTime).toMillis();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        StringBuilder sb = new StringBuilder();
        if (day > 0L) {
            sb.append(day).append("天");
        }
        if (hour > 0L) {
            sb.append(hour).append("小时");
        }
        sb.append(min).append("分钟");
        return sb.toString();
    }

    public static String getWeekChn(LocalDateTime time) {
        switch (time.getDayOfWeek().getValue()) {
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            case 7:
                return "星期日";
            default:
                return "";
        }
    }

    public static String getBeforeChn(LocalDateTime time) {
        if (time == null) return "";
        Duration duration = Duration.between(time, LocalDateTime.now());
        long second = duration.toSeconds();
        if (second <= 60L)
            return second + "秒前";
        if (second <= 3600L) {
            second /= 60L;
            return second + "分钟前";
        }
        if (second <= 86400L) {
            second = second / 60L / 60L;
            return second + "小时前";
        }
        if (second <= 864000L) {
            second = second / 60L / 60L / 24L;
            return second + "天前";
        }
        return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }

    public static long getBetweenDays(String date1, String date2) {
        if (date1 == null || date1.equals("") || date2 == null || date2.equals("")) {
            return 0L;
        }
        try {
            return Duration.between(TKConvert.toLocalDate(date1), TKConvert.toLocalDate(date2)).toDays();
        } catch (Exception ex) {
            return 0L;
        }
    }

    public static long getBetweenDays(LocalDateTime sourceTime, LocalDateTime targetTime) {
        return Duration.between(sourceTime.toLocalDate(), targetTime.toLocalDate()).toDays();
    }
}
